/**
 * author: mengyao.xi
 * http通用工具函数
 */
import axios from 'axios';
import { message } from 'antd';
axios.defaults.timeout = 15000;
axios.defaults.baseURL = '/'
/**
 * http request 拦截器
*/
axios.interceptors.request.use(
  (config) => {
    // console.log('token', localStorage.getItem('token'))
    // config.data = JSON.stringify(config.data);
    config.headers = {
      // Accept: 'application/json;charset=UTF-8',
      'Content-Type': 'application/json',
      // 'Authorization': 'Bearer APP_PORTAL_S_stn92pzP2Hkw53fDfM29zWqujXmRyxVq',
      'Authorization': 'Bearer ' + localStorage.getItem('token') || 'Bearer APP_PORTAL_S_stn92pzP2Hkw53fDfM29zWqujXmRyxVq',
    };
    if(config.url == '/solar-health-degree-backend/v1.0/exportHealthBreakDownExcel') {
      config.responseType = "blob"
    }
    if(config.url == '/solar-health-degree-backend/v1.0/queryHealthAlertsExport') {
      config.responseType = "blob"
    }
    // umbrella.getLocalStorage('userToken') && (config.headers.UserToken = umbrella.getLocalStorage('userToken'));
    return config;
  },
  (error) => {
    return Promise.reject(error);
  }
);

axios.interceptors.response.use(
  (config) => {
    // console.log('axios response 111', config)
    let data = config;
    if (config.data && config.data.code == 200) {
      data = config.data.data
    }else {
      // console.log('axios response', config)
      if(config.data.submsg || config.data.msg) {
        message.error(config.data.submsg || config.data.msg);
      }
      // if (config.data.code == 2001) { // 用户未登录
      //   console.log('未登录')
      //   // umbrella.removeLocalStorage('userToken');
      //   window.location.href = '/'
      // }
    }
    // console.log('response', config.data)
    return config.data;
  },
  (error) => {
    return Promise.reject(error);
  }
);

/**
 * 公用get请求
 * @param url       接口地址
 * @param msg       接口异常提示
 * @param headers   接口所需header配置
 */
export const get = ({ url, msg = '接口异常', config }) =>
  axios
    .get(url, config)
    .then((res) => res.data)
    .catch((err) => {
      console.log(err);
      message.error(msg);
    });

/**
 * 公用post请求
 * @param url       接口地址
 * @param data      接口参数
 * @param msg       接口异常提示
 * @param headers   接口所需header配置
 */

export const post = ({ url, data={}, msg = '接口异常', config}) =>
  axios
    .post(url, data, config)
    .then((res) => res.data)
    .catch((err) => {
      console.log('err', err);
      if (err && err.response && err.response.data && err.response.data.code != 200) {
        if (err.response.data.submsg) {
          message.error(err.response.data?.submsg)
        } else{
          if(err.response.status === 504 || err.response.status === 503) {
            message.error('健康度分析服务发生异常');
          } else {
            message.error('健康度分析接口请求超时异常');
          }
        }
      } else {
        message.error(err?.message);
      }
    });
export const postPage = ({ url, data={}, msg = '接口异常', config}) =>
    axios
      .post(url, data, config)
      .then((res) => res)
      .catch((err) => {
        console.log('err', err);
        console.log(url);
        if (err && err.response && err.response.data && err.response.data.code != 200) {
          if (err.response.data.submsg) {
            message.error(err.response.data?.submsg)
          } else{
            if(err.response.status === 504 || err.response.status === 503) {
              message.error('健康度分析服务发生异常');
            } else {
              message.error('健康度分析接口请求超时异常');
            }
          }
        } else {
          console.log(err);
          message.error(err?.message);
        }
      });

// export function post(url, data){
//   console.log('post', url, data)
//   return new Promise((resolve,reject) => {
//     axios.post(url.url,data.data)
//     .then(response => {
//       resolve(response.data);
//     }, err => {
//       reject(err)
//     })
//   })
// }

/**
 * 公用put请求
 * @param url       接口地址
 * @param data      接口参数
 * @param msg       接口异常提示
 * @param headers   接口所需header配置
 */
export const put = ({ url, data, msg = '接口异常', config }) =>
  axios
    .put(url, data, config)
    .then((res) => res.data)
    .catch((err) => {
      console.log(err);
      message.error(msg);
    });

/**
 * 公用delete请求
 * @param url       接口地址
 * @param msg       接口异常提示
 * @param headers   接口所需header配置
 */
export const deleteMethod = ({ url, msg = '接口异常', config }) =>
  axios
    .delete(url, config)
    .then((res) => res.data)
    .catch((err) => {
      console.log(err);
      message.error(msg);
    });

export const downLoad = ({ url, data={}, msg = '接口异常', config}) =>
  axios
    .post(url, data, config)
    .then((res) => res)
    .catch((err) => {
      console.log('err', err);
      if(err?.response?.data?.type) {
        err.response.data.text().then(res=> message.error(res))
      } else {
        if (err && err.response && err.response.data && err.response.data.code != 200) {
          message.error(err.response.data.submsg)
        } else {
          message.error(msg);
        }
      }
    });
